<template>
	<div>
		<!-- 选择承诺卡弹窗 -->
    <el-dialog 
      title="选择承诺卡"
      append-to-body
      :visible.sync="choosePromiseDialog"
      :close-on-click-modal="false" 
      :before-close="handleCloseChoosePromise"
      width="60%"
    >

      <!-- 表格内容 -->
      <avue-crud 
        :option="promiseOption"
        :data="promiseData.filter((item) => item.cnkbh.includes(filterPromiseName))"
        @selection-change="promiseSelectionChange"
        ref="promiseCrud"
      >
        <template slot="menuLeft">
          <el-input
            placeholder="请输入承诺卡编号"
            v-model="filterPromiseName"
            size="small">
          </el-input>
        </template>
      </avue-crud>

      <!-- 按钮 -->
      <div class="choose_user_footer">
        <el-button 
          type="primary"
          size="small"
          @click="choosePromise()">
          确 认
        </el-button>
        <el-button 
          size="small"
          @click="cancleChoosePromise()">
          取 消
        </el-button>
      </div>
    </el-dialog>
	</div>
</template>

<script>
import { getPromiseCardList } from "@/api/saferisk/riskpoint";

const tenantId = JSON.parse( localStorage.getItem("saber-tenantId") ).content;

export default {
  props: {
    choosePromiseDialog: {default: false},
    filterPromiseName: {default: ""},
    selectPromiseTags: {default: []}
  },
  data() {
    return {
      promiseData: [],
      promiseSelectionList: [],
      promiseOption: {
        calcHeight: 30,
        tip: false,
        index: true,
        selection: true,
        addBtn: false,
        refreshBtn: false,
        columnBtn: false,
        menu: false,
        column: [
          {
            label: "承诺卡编号",
            prop: "cnkbh",
            align: "center"
          },
          {
            label: "承诺人",
            prop: "cnr",
            align: "center",
            type: "select",
            dicUrl: "/api/riskPoint/riskmanagement/getInternalStaff",
            props: {
              label: "realName",
              value: "id",
            }
          },
          {
            label: "主管领导",
            prop: "zgld",
            align: "center",
            type: "select",
            dicUrl: "/api/riskPoint/riskmanagement/getInternalStaff",
            props: {
              label: "realName",
              value: "id",
            }
          },
          {
            label: "岗位名称",
            prop: "gwmc",
            align: "center",
            type: "select",
            dicUrl: "/api/blade-system/post/select?tenantId=" + tenantId,
            props: {
              label: "postName",
              value: "id",
            }
          },
          {
            label: "承诺日期",
            prop: "cnrq",
            align: "center"
          }
        ]
      }
    }
  },
  watch: {
    choosePromiseDialog(){
      if(this.choosePromiseDialog === true){
        this.initData();
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化评承诺卡弹窗
    initData() {
      this.getPromiseCards();
    },

    // 获取承诺卡列表
    getPromiseCards() {
      getPromiseCardList().then(res => {
        this.promiseData = res.data.data;
      }).then(() => {
        this.checkSelectedPromises();
      });
    },

    // 勾选已在列表中的承诺卡
    checkSelectedPromises() {
      if(this.selectPromiseTags.length !== 0) {
        let promiseIds = [];
        this.selectPromiseTags.forEach(item => {
          promiseIds.push(item.id);
        });
        this.$nextTick(() => {
          this.promiseData.forEach(row => {
            if(promiseIds.indexOf(row.id) >= 0 && this.$refs.promiseCrud !== undefined) {
              this.$refs.promiseCrud.toggleRowSelection(row, true);
            }
          })
        });
      } else {
        if(this.$refs.promiseCrud !== undefined) {
          this.$refs.promiseCrud.toggleSelection();
        }
      }
    },

    // 勾选承诺卡
    promiseSelectionChange(list) {
      this.promiseSelectionList = list;
    },

    // 确认勾选人员
    choosePromise() {
      if(this.promiseSelectionList.length === 0) {
        this.$message.warning("请选择承诺卡!");
        return;
      }
      if(this.promiseSelectionList.length > 3) {
        this.$message.warning("最多选择3张承诺卡!");
        return;
      }
      this.$emit('choosePromise', this.promiseSelectionList);
    },

    // 关闭选择人员弹窗
    cancleChoosePromise() {
      this.$emit('cancleChoosePromise');
    },

    // 关闭选择人员弹窗
    handleCloseChoosePromise(done) {
      done();
      this.$emit('handleCloseChoosePromise');
    },

  }
}
</script>